#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int n, q, c, c1;
int a[MAXN], fa[MAXN][18], fat[MAXN][18], siz[MAXN], maxs[MAXN];
int ord[MAXN], dfn[MAXN], low[MAXN], times;
vector<int> G[MAXN]; int ans[MAXN]; vector<pair<int, int>> qu[MAXN];
int ht[1 << 15];
void dfs(int u, int f) {
	fa[u][0] = f; fat[u][0] = u == f ? 0 : 1 << a[f]; ord[dfn[u] = ++times] = u; siz[u] = 1;
	for (int i = 1; i <= 17; i++) {
		fa[u][i] = fa[fa[u][i - 1]][i - 1], fat[u][i] = fat[u][i - 1] | fat[fa[u][i - 1]][i - 1];
	}
	for (int v : G[u]) dfs(v, u), siz[u] += siz[v], maxs[u] = maxs[u] && siz[maxs[u]] > siz[v] ? maxs[u] : v;
	low[u] = times;
}
int lat[MAXN];
void ins(int x) {
	// printf("ins %d\n", x);
	for (int i = 0; i < 1 << c1; i++) if ((x & i) == 0) ht[x | i]++;
}
void era(int x) {
	// printf("era %d\n", x);
	for (int i = 0; i < 1 << c1; i++) if ((x & i) == 0) ht[x | i]--;
}
int que(int x) {
	// printf("que %d\n", x); 
	int ans = 0; for (int i = 0; i < 1 << (c - c1); i++) {
		if ((x & (i << c1)) == (i << c1)) ans += ht[x ^ i << c1];
	} return ans;
}
void allins(int u, int w) {ins(lat[u] = w |= 1 << a[u]); for (int v : G[u]) allins(v, w);}
void fix(int u, int c) {
	if (a[u] == c) return; era(lat[u]); ins(lat[u] |= 1 << c); for (int v : G[u]) fix(v, c);
}
void dfs2(int u) {
	// printf("dfs2 %d\n", u);
	for (int v : G[u]) if (v != maxs[u]) {
		dfs2(v); for (int i = dfn[v]; i <= low[v]; i++) era(lat[ord[i]]);
	}
	if (maxs[u]) dfs2(maxs[u]), fix(maxs[u], a[u]);
	ins(lat[u] = 1 << a[u]);
	// printf("reins %d\n", u);
	for (int v : G[u]) if (v != maxs[u]) allins(v, 1 << a[u]);
	for (auto &x : qu[u]) ans[x.second] = que(x.first);
}
int main() {
	freopen("tree.in", "r", stdin); freopen("tree.out", "w", stdout);
	scanf("%d %d %d", &n, &q, &c);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 2; i <= n; i++) {int f; scanf("%d", &f), G[f].push_back(i);}
	double av = 1e18; for (int i = 0; i <= c; i++) {
		double co = log2(n) * 2 * (1 << i) + (1 << (c - i));
		if (av > co) av = co, c1 = i;
	}
	fprintf(stderr, "c = %d, c1 = %d\n", c, c1);
	dfs(1, 1); for (int i = 1; i <= q; i++) {
		int s, u; scanf("%d %d", &s, &u);
		if ((s >> a[u] & 1) == 0) {ans[i] = 1; continue;}
		for (int i = 17; ~i; i--) if ((fat[u][i] & s) == fat[u][i]) u = fa[u][i];
		qu[u].push_back({s, i});
		// if (i == 36) printf("??? %d %d %d\n", i, s, u);
	}
	dfs2(1); for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
/*
run tree < tree1.in > tree1.out
run tree < tree2.in > tree2.out
run tree < tree3.in > tree3.out
run tree < tree4.in > tree4.out
run tree < tree5.in > tree5.out
run2 tree < tree5.in > tree5.out
diff tree1.out tree1.ans -Z
diff tree2.out tree2.ans -Z
diff tree3.out tree3.ans -Z
diff tree4.out tree4.ans -Z
diff tree5.out tree5.ans -Z
*/